package link.doublyLinkedListSentinel;

import java.util.Iterator;

/**
 * @author czt
 * @version 1.0
 * @since 2024/12/12
 * <p>
 * 带哨兵的双向链表
 */
public class DoublyLinkedListSentinel implements Iterable<Integer> {


    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head.next;
            @Override
            public boolean hasNext() {
                return p != tail;

            }

            @Override
            public Integer next() {
                Integer value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    static class Node {
        // 前指针
        Node prev;

        // 值
        int value;

        // 后指针
        Node next;

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    // 头哨兵
    private Node head;

    //尾哨兵
    private Node tail;

    public DoublyLinkedListSentinel() {
        head = new Node(null, 666, null);
        tail = new Node(null, 666, null);

        head.next = tail;
        tail.prev = head;
    }

    /**
     * 根据索引找节点
     *
     * @param index 索引
     * @return 节点
     */
    private Node findNode(int index) {
        int i = -1;
        for (Node p = head; p != tail; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }

    /**
     * 向指定位置插入新节点
     *
     * @param value 待插入节点值
     * @param index 待插入节点索引
     */
    public void insert(int value, int index) {
        // 先找到待插入节点的上一个节点和下一个节点
        Node prev = findNode(index - 1);

        if (prev == null) {
            throw new RuntimeException("索引异常");
        }

        Node next = prev.next;

        prev.next = new Node(prev, value, next);
        next.prev = new Node(prev, value, next);
    }

    /**
     * 向链表头部插入元素
     *
     * @param value 待插入元素
     */
    public void addFirst(int value) {
        insert(value, 0);
    }

    /**
     * 删除指定位置的节点
     *
     * @param index 待删除节点索引
     */
    public void remove(int index) {
        // 待删除节点
        Node removed = findNode(index);
        if (removed == null || removed == tail || removed == head) {
            throw new RuntimeException("索引异常");
        }

        // 待删除节点的上一个节点
        Node prev = removed.prev;

        // 待删除节点的下一个节点
        Node next = removed.next;

        prev.next = next;
        next.prev = prev;
    }

    /**
     * 删除头节点
     */
    public void removeFirst() {
        remove(0);
    }

    /**
     * 链表尾部插入新节点
     *
     * @param value 待插入节点值
     */
    public void addLast(int value) {
        // 最后一个节点
        Node last = tail.prev;

        last.next = new Node(last, value, tail);
        tail.prev = new Node(last, value, tail);
    }

    /**
     * 删除链表尾部的节点
     */
    public void removeLast() {
        // 待删除节点
        Node removed = tail.prev;

        // 待删除节点的上一个节点
        Node prev = removed.prev;
        if (prev == head) {
            throw new RuntimeException("链表为空");
        }

        prev.next = tail;
        tail.prev = prev;
    }

}
